package com.qdlc.p2p.biz.processor.task;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.qdlc.p2p.biz.processor.event.CollectionTradeNotifyValueEvent;
import com.qdlc.p2p.biz.service.AccountService;
import com.qdlc.p2p.biz.service.BorrowService;
import com.qdlc.p2p.biz.service.BorrowTenderService;
import com.qdlc.p2p.common.constant.BorrowStatus;
import com.qdlc.p2p.common.constant.BorrowTenderStatus;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.dal.dto.Borrow;
import com.qdlc.p2p.dal.dto.BorrowTender;
import com.qdlc.p2p.dal.dto.Tpp;
import com.qdlc.p2p.dal.dto.TppTradeLog;
import com.qdlc.p2p.dal.model.BorrowModel;

/**
 * 代收/代付异步通知： 投标、自动投标
 * @author linya 2016-01-13
 */
public abstract class AbstractCollectionTradeNotifyTendeTaskProcessor extends AbstractCollectionTradeNotifyTaskProcessor {

	private static final Logger logger = Logger.getLogger(AbstractCollectionTradeNotifyTendeTaskProcessor.class);
	
	@Resource
	BorrowTenderService borrowTenderService;
	
	@Resource
	BorrowService borrowService;

	@Resource
	AccountService accountService;
	
	/**
	 * 投标、自动投标交易成功后不需要处理
	 * @return
	 */
	public boolean supprotTradeFinished(String serviceType){
		return false;
	}
	
	@Override
	protected void doPay(TppTradeLog tppTradeLog) {
		BorrowModel bm = new BorrowModel();
		long borrowId = tppTradeLog.getBorrowId();
		String tppUserCustId = tppTradeLog.getTppUserCustId();
		bm.setId(borrowId);
		bm.setBidNo(tppTradeLog.getOrderNo());
		bm.setTenderBilNo(tppTradeLog.getOrderNo());
		bm.setTenderBilDate(DateUtil.dateStr3(tppTradeLog.getTradeTime()));
		// borrowTender的account金额包含现金、红包、体验券
		bm.setMoney(tppTradeLog.getAmount());
		bm.setTppUserCustId(tppUserCustId);
		doPay(tppTradeLog, bm);
	}

	protected abstract void doPay(TppTradeLog tppTradeLog, BorrowModel bm);

	protected void doFail(TppTradeLog tppTradeLog, Tpp tpp, CollectionTradeNotifyValueEvent event){
		
		String loggerPrefix = getLoggerPrefix(tppTradeLog.getTradeType());
		
		String tenderBilNo = tppTradeLog.getOrderNo();
		BorrowTender borrowTender = borrowTenderService.find(tenderBilNo);
		if(borrowTender == null){
			return;
		}
		
		Borrow borrow = borrowService.find(borrowTender.getBorrow().getId());
		if (BorrowStatus.STATUS_TRIAL_PASSED == borrow.getStatus()) {
			if(borrowTender.getStatus() !=  2) {
				logger.info(MessageFormat.format("++++ {0}订单号{1}投资记录回滚业务处理开始", loggerPrefix, event.getOutOrderNo()));
				borrowTender.setStatus(BorrowTenderStatus.FAIL);
				borrowTenderService.update(borrowTender);
				double accountYes = BigDecimalUtil.sub(borrow.getAccountYes(), borrowTender.getAccount());
				borrow.setAccountYes(accountYes);
				borrowService.update(borrow);
				// 解冻投标资金
				long userId = borrowTender.getUser().getUserId();
				accountService.unFreeze(userId, userId, borrowTender.getRealAmount());
			} else{
				logger.info(MessageFormat.format("++++ {0}订单号{1}投资记录回滚业务已被处理", loggerPrefix, event.getOutOrderNo()));
			}
		} else {
			tppTradeLog.setMemo(loggerPrefix + "交易结果失败，投资记录回滚失败！");
		}
	}
	
}
